<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->

<gio-banner-info>
  Each API is monitored by routine interrogation of its health-check endpoint. After receiving a health-check request, the API backend
  performs the necessary verifications via probes to return its status.
</gio-banner-info>

<mat-card>
  <mat-card-content>
    <div>
      <gio-quick-time-range [formControl]="timeFrameControl" (onRefreshClicked)="onRefreshClicked()"></gio-quick-time-range>
      <!-- button to set filter-->
      <button mat-stroked-button class="only-configured-button" (click)="onOnlyHCConfigured()">Filter by Heath-Check enabled only</button>
    </div>
    <gio-banner
      *ngIf="allApisHCStatus"
      class="apiHCReportBanner"
      [type]="
        !allApisHCStatus.isLoading && (allApisHCStatus.inError > 0 || allApisHCStatus.inWarning > 0)
          ? allApisHCStatus.inError > 0
            ? 'error'
            : 'warning'
          : 'info'
      "
    >
      API health-check Report
      <span gioBannerBody>
        <ng-container *ngIf="allApisHCStatus.isLoading">Loading...</ng-container>
        <ng-container *ngIf="!allApisHCStatus.isLoading">
          <ng-container *ngIf="allApisHCStatus.inError === 0 && allApisHCStatus.inWarning === 0">All APIs operational</ng-container>
          <ng-container *ngIf="allApisHCStatus.inError > 0"
            >{{ allApisHCStatus.inError == 1 ? '1 API is in error' : allApisHCStatus.inError + ' APIs are in error' }} (HealthCheck
            availability <= 80%)<br
          /></ng-container>
          <ng-container *ngIf="allApisHCStatus.inWarning > 0"
            >{{ allApisHCStatus.inWarning == 1 ? '1 API is in warning' : allApisHCStatus.inWarning + ' APIs are in warning' }} (HealthCheck
            availability <= 95%)</ng-container
          >
        </ng-container>
      </span>
    </gio-banner>
  </mat-card-content>

  <gio-table-wrapper
    [searchLabel]="'Search APIs | name:&quot;My api *&quot; ownerName:admin'"
    [length]="apisTableDSUnpaginatedLength"
    [filters]="filters"
    (filtersChange)="onFiltersChanged($event)"
  >
    <table mat-table matSort [dataSource]="apisTableDS" id="apisTable" aria-label="Apis table">
      <!-- Picture Column -->
      <ng-container matColumnDef="picture">
        <th mat-header-cell *matHeaderCellDef id="picture"></th>
        <td mat-cell *matCellDef="let element">
          <gio-avatar [src]="element.picture" [name]="element.name + ' ' + element.version" [size]="32" [roundedBorder]="true"></gio-avatar>
        </td>
      </ng-container>

      <!-- Display Name Column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef mat-sort-header id="name">Name</th>
        <td mat-cell *matCellDef="let element" title="{{ element.name }} ({{ element.version }})">
          {{ element.name }} ({{ element.version }})
        </td>
      </ng-container>

      <!-- Display States Column -->
      <ng-container matColumnDef="states">
        <th mat-header-cell *matHeaderCellDef id="states"></th>
        <td mat-cell *matCellDef="let element">
          <mat-icon
            *ngIf="element.state === 'STARTED'"
            matTooltip="Started"
            class="states__api-started"
            size="20"
            svgIcon="gio:play-circle"
          ></mat-icon>
          <mat-icon
            *ngIf="element.state !== 'STARTED'"
            matTooltip="Stopped"
            class="states__api-not-started"
            size="20"
            svgIcon="gio:stop-circle"
          ></mat-icon>
          <mat-icon
            *ngIf="element.isNotSynced$ | async"
            matTooltip="API out of sync"
            class="states__api-is-not-synced"
            size="20"
            svgIcon="gio:refresh-cw"
          ></mat-icon>
          <mat-icon
            *ngIf="element.lifecycleState === 'PUBLISHED'"
            matTooltip="Published"
            class="states__api-published"
            size="20"
            svgIcon="gio:cloud-published"
          ></mat-icon>
          <mat-icon
            *ngIf="element.lifecycleState !== 'PUBLISHED'"
            matTooltip="Unpublished"
            class="states__api-not-published"
            size="20"
            svgIcon="gio:cloud-unpublished"
          ></mat-icon>
          <mat-icon
            *ngIf="element.origin === 'kubernetes'"
            matTooltip="Kubernetes Origin"
            class="states__api-origin"
            size="20"
            svgIcon="gio:kubernetes"
          ></mat-icon>
          <span
            *ngIf="element.workflowBadge"
            [ngClass]="element.workflowBadge.class"
            class="states__api-workflow-badge"
            [matTooltip]="element.workflowBadge.text"
          >
            {{ element.workflowBadge.text }}
          </span>
        </td>
      </ng-container>

      <!-- API Availability Column -->
      <ng-container matColumnDef="availability">
        <th mat-header-cell *matHeaderCellDef id="availability">API Availability</th>
        <td mat-cell *matCellDef="let element">
          <div class="chart" *ngIf="$any(element.availability$ | async); let availability">
            <ng-container *ngIf="availability.type === 'configured'">
              <gio-circular-percentage
                class="chart__gauge"
                matTooltip="HealthCheck availability"
                [score]="availability.healthCheckAvailability"
              ></gio-circular-percentage>
              <div class="chart__health-availability-timeframe">
                <health-availability-time-frame [option]="availability.healthAvailabilityTimeFrame"></health-availability-time-frame>
              </div>
            </ng-container>
            <ng-container *ngIf="availability.type === 'no-data'"> No data to display </ng-container>
            <ng-container *ngIf="availability.type === 'not-configured'"> Health-check has not been configured </ng-container>
          </div>
        </td>
      </ng-container>

      <!-- Actions Column -->
      <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef id="actions"></th>
        <td mat-cell *matCellDef="let element">
          <div class="actions__edit">
            <button
              (click)="onViewHealthCheckClicked(element)"
              mat-icon-button
              aria-label="Button to view API Health-check"
              matTooltip="View API Health-check"
            >
              <mat-icon svgIcon="gio:eye-empty"></mat-icon>
            </button>
          </div>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

      <!-- Row shown when there is no data -->
      <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
        <td
          *ngIf="!isLoadingData && apisTableDS.length === 0"
          class="mat-mdc-cell mdc-data-table__cell"
          [attr.colspan]="displayedColumns.length"
        >
          {{ 'No APIs to display.' }}
        </td>
        <td *ngIf="isLoadingData" class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">
          {{ 'Loading...' }}
        </td>
      </tr>
    </table>
  </gio-table-wrapper>
</mat-card>
